/* Copyright (C) 2014-2015 Mammoth Inc. */

var m = new M("oj");

$(function() {
	intervals = [parseInt($(".first-circle").data("target")) / 100, parseInt($(".second-circle").data("target")), parseInt($(".third-circle").data("target")) / 100];
	counter = 1;
	firstScroll = true;
	var circleWidth = parseInt(Math.pow($(window).width(), 1 / 2) * 3);
	$(".oj-circle-label, .green-font, .white-font, .board-name").css("font-size", parseInt(Math.pow($(window).width(), 1 / 3) * 1.5) + "px");
	diffLevelMap = {
		"0": "初级",
		"1": "中级",
		"2": "高级"
	};
	var numberPerPage = 20;
	var refreshTable = function(data, pageNumber) {
		$("#chtable tbody").html('');
		for (var i = 0; i < data.length; i++) {
			$("#chtable tbody").append('<tr>' +
				'<td style="width:10%;">' + ((pageNumber - 1) * numberPerPage + i + 1) + '</td>' +
				'<td style="width:50%;text-align:left;"><a href="javascript:void(0);" data-id="' + data[i].infor_id + '">' + data[i].title + '</a></td>' +
				'<td style="width:15%;">' + diffLevelMap[data[i].diff_level] + '</td>' +
				'<td style="width:15%;">' + data[i].submit + '</td>' +
				'<td style="width:10%;">' +
				'<span style="display:inline-block;height:20px;width:70px;background-image:url(\'http://s.mengma.com/img/mm_oj_circle_progress.png\');background-repeat:no-repeat;background-size:20px 520px;background-position:left -' + (parseInt(parseInt(data[i].pass_rate.replace("%", "")) / 4) * 20) + 'px;">&nbsp;' + data[i].pass_rate + '</span></td>' +
				'</tr>');
		}
		$("td").find("a").click(function() {
			var that = $(this);
			if ($(".mm_user").length > 0) {
				window.open("http://oj.mengma.com/quiz/" + that.data("id"));
			} else {
				if (confirm("还未登录，请先登录")) {
					var link = window.location.href;
					window.location.href = "http://passport.mengma.com/?continue=" + link;
				}
			}
		});
	}
	var getTableContent = function(data) {
		var first = data;
		$.ajax({
			type: "POST",
			data: data,
			url: "/api/chlist/getlist",
			dataType: "json",
			success: function(data) {
				if (data !== "false") {
					$(".pagination-holder").pagination({
						items: data.length,
						itemsOnPage: numberPerPage,
						prevText: "上一页",
						nextText: "下一页",
						onPageClick: function(pageNumber, event) {
							refreshTable(data.slice((pageNumber - 1) * numberPerPage, pageNumber * numberPerPage), pageNumber);
							$(window).scrollTop(0)
						},
						cssStyle: 'light-theme',
						displayedPages: 3,
						edges: 2
					});
					refreshTable(data.slice((1 - 1) * numberPerPage, 1 * numberPerPage), 1);
				}
			}
		});
	}
	getTableContent({});
	$("#oj_avatar").width(circleWidth).height(circleWidth).css("border-radius", circleWidth / 2);
	$(".first-circle").knob({
		'min': 0,
		'max': 100,
		'width': circleWidth,
		'readOnly': true,
		'thickness': 0.15,
		'fgColor': '#88C774',
		'bgColor': '#27586D',
		'inputColor': '#FFFFFF',
		'format': function(val) {
			return val + "%";
		}
	});
	$(".second-circle").knob({
		'min': 0,
		'max': intervals[1],
		'width': circleWidth,
		'readOnly': true,
		'thickness': 0.15,
		'fgColor': '#F49352',
		'bgColor': '#27586D',
		'inputColor': '#FFFFFF',
	});
	$(".third-circle").knob({
		'min': 0,
		'max': 100,
		'width': circleWidth,
		'readOnly': true,
		'thickness': 0.15,
		'fgColor': '#5AC8D9',
		'bgColor': '#27586D',
		'inputColor': '#FFFFFF',
		'format': function(val) {
			return val + "%";
		}
	});
	gogogo = function() {
		$.map($(".first-circle, .second-circle, .third-circle"), function(elem, i) {
			$(elem).val(intervals[i] * counter).trigger("change");
		});
		counter++;
		if (counter <= 100) {
			interval = counter > 90 ? counter : Math.pow(counter, 1 / 2);
			setTimeout("gogogo()", interval);
		}
	}
	gogogo();
	$(".center-block").width(circleWidth).height($("canvas").width());
	$(".oj-circle-label").height(40);
	$("#slide-btn").click(function() {
		if (firstScroll) {
			firstScroll = !firstScroll;
		}

		if ($(this).attr("src").indexOf("up") != -1) {
			$(this).attr("src", "http://s.mengma.com/img/mm_oj_arrow_down.png");
		} else {
			$(this).attr("src", "http://s.mengma.com/img/mm_oj_arrow_up.png");
		}
		$("#scroll-hide").slideToggle("normal", function() {
			if ($('.small-bar').css('opacity') == '0') {
				$("#main-container").animate({
					'margin-top': '126px'
				});
				$('.small-bar').fadeTo(1000, 1);
			} else {
				$("#main-container").animate({
					'margin-top': ($(".mm_navbar").eq(0).height() + $(".mm_navbar").eq(1).height() + 5) + 'px'
				});
				$('.small-bar').fadeTo(1000, 0);
			}
		});

	})
	var lastScrollTop = 0;
	$(window).scroll(function(e) {
		if (firstScroll) {
			var st = $(this).scrollTop();
			if (st > lastScrollTop) {
				$(window).scrollTop(0)
				firstScroll = !firstScroll;
				$("#slide-btn").click();
			}
			lastScrollTop = st;
		}
	})
	$.ajax({
		type: "POST",
		url: "/api/user/getUserRankList",
		dataType: "json",
		success: function(data) {
			$("#board tbody").html('');
			for (var i = 0; i < data.length; i++) {
				$("#board tbody").append('<tr>' +
					'<td>' + (i <= 2 ? '<span class="label label-warning">' + (i + 1) + '</span>' : (i + 1)) +
					'</td>' +
					'<td style="width:50px;"><img style="height:50px;width:50px;border-radius:25px;float:left;" src="' + data[i].avatar + '" alt=""></td><td class="board-name">' + data[i].name + '</td>' +
					'<td class="green-font" style="text-align:right;">' + data[i].score + '</td>' +
					'</tr>');
			}
			$(".board-name").css("text-align", "left");
		}
	});
	$.ajax({
		type: "POST",
		url: "/api/ojTag/getCatTag",
		dataType: "json",
		success: function(data) {
			$("#label-list").html('');
			$("#label-list").append('<button type="button" class="btn btn-default oj-label">全部</button>');
			for (var i = 0; i < data.length; i++) {
				if (i == 3) {
					$("#label-list").append('<button type="button" id="unfold-labels" data-others="' + data.slice(3) + '" class="btn btn-default oj-label">...</button>');
					$("#unfold-labels").click(function() {
						var labels = $(this).data("others").split(",");
						for (var i = 0; i < labels.length; i++) {
							$(this).parent().append('<button type="button" class="btn btn-default oj-label">' + labels[i] + '</button>')
						}
						$(this).remove();
					});
					break;
				}
				$("#label-list").append('<button type="button" class="btn btn-default oj-label">' + data[i] + '</button>');
			}
			$(".btn-group").not($("#unfold-labels")).on("click", ".oj-label", function() {
				if ($(this).text() === "全部") {
					window.location.hash = '';
					window.location.reload();
					return;
				}
				$(".oj-label-now").removeClass("oj-label-now");
				$(this).addClass("oj-label-now");
				getTableContent({
					tag: $(this).text()
				});
			})
		}
	});
	$(".borderless td").css("borderTopWidth", "0");
	$("#scroll-hide").css({
		'marginTop': $(window).height() / 20 * 2,
		'marginBottom': $(window).height() / 20 / 2
	});
	$("#main-container").animate({
		'margin-top': ($(".mm_navbar").eq(0).height() + $(".mm_navbar").eq(1).height() + 5) + 'px'
	});
})